package c.c.k.service.impl;

import c.c.k.config.StockConfig;
import c.c.k.emums.JYSEnum;
import c.c.k.pojo.Stock;
import c.c.k.pojo.StockBase;
import c.c.k.repository.StockBaseRepository;
import c.c.k.repository.StockRepository;
import c.c.k.service.StockBaseService;
import c.c.k.service.StockService;
import c.c.k.util.StockData;
import c.c.k.util.StockUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by chenck on 2022/3/19.
 */
@Service
@Slf4j
public class StockBaseServiceImpl implements StockBaseService {

    @Autowired
    private StockBaseRepository stockBaseRepository;
    @Autowired
    private StockConfig stockConfig;

    @Override
    public void initStockBase() {
        this.fetchStock(stockConfig.getBaseUrl());
        this.fetchStock(stockConfig.getBaseUrlHK());
    }

    private void fetchStock(String url){
        log.info("fetch url {}", url);

        StockUtil.parseJsonArray(url, jsonArray -> {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String code = jsonObject.getStr("dm");
                String name = jsonObject.getStr("mc");
                String jys = jsonObject.getStr("jys");

                this.addStockBase(StockBase.builder().code(code).name(name).jys(jys).shiZhi(0D).build());
            }
        });
    }

    @Override
    public void addStockBase(StockBase stock) {
        StockBase one = stockBaseRepository.findByCode(stock.getCode());
        if(one != null){
            return;
            //stock.setId(one.get().getId());
        }
        stockBaseRepository.save(stock);
    }

    @Override
    public List<StockBase> listGtShiZhi(Double shiZhi) {
        return stockBaseRepository.findAllByShiZhiGreaterThan(shiZhi);
    }

    @Override
    public void updateShiZhi() {
        this.fetchEverydayByUrl(JYSEnum.SH, stockConfig.getTradeNowUrl());
        this.fetchEverydayByUrl(JYSEnum.SZ, stockConfig.getTradeNowUrl());
        this.fetchEverydayByUrl(JYSEnum.HK, stockConfig.getTradeNowUrlHK());
    }

    private  void fetchEverydayByUrl(JYSEnum jysEnum, String url){
        stockBaseRepository.findAllByJys(jysEnum.getDesc()).forEach(s-> {
            if(s.getShiZhi()>0)return;
            try {
                if(s.getShiZhi().equals(0D)) {
                    String data = HttpUtil.get(String.format(url, s.getCode()));
                    JSONObject jsonObject = JSONUtil.parseObj(data);
                    Double shiZhi = jsonObject.getDouble("sz");

                    s.setShiZhi(shiZhi);
                    stockBaseRepository.save(s);
                }
            }catch (Exception ex){
                ex.printStackTrace();
            }
        });
    }
}
